PRAYER_CONTAINERS = [];
CONTAINER_WRAPPER_ID = "prayer_container_wrapper";
ACTIVATED_CONTAINER_ID = "";
MIN_X = null;
MAX_X = 0;
MIN_Y = null;
MAX_Y = 0;
STEP_Y = 238;
STEP_X = 960;

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var resetForm = function() {
	var forms = document.getElementsByTagName("form");
	for(var i=0; i<forms.length; i++) {
		forms[i].reset();
	}
}

var smartFocus = function() {
	var candidates = $$(".smart_focus");
	if(!candidates.is_empty()) candidates[0].focus();
}

var restrictionNotifier = function() {
	var fields = $$(".length_restriction") 
	for(var i=0; i<fields.length; i++) {
		var field = fields[i];
		var notifier = document.createElement("div");
		notifier.id = field.id + "_notifier";
		notifier.className = "restriction_notifier";
		notifier.style.width = field.clientWidth + "px";
		notifier.innerHTML = "<span id='" + field.id + "_character_count'>" + field.value.length + "</span> / " + field.getAttribute("rel");

		// top
		field.parentNode.insertBefore(notifier, field);

		// bottom
		// if(field.nextSibling) field.parentNode.insertBefore(notifier, field.nextSibling);
		// else field.parentNode.appendChild(notifier);

		OnEvent.add(field, "keyup", BindEvent(this, function(_e) {
			var _event = new Eventor(_e);
			if(_event._keycode() == 13) return;

			var field = _event._node();
			var cur_count = field.value.length;
			var max_count = Number(field.getAttribute("rel"));

			if(cur_count > max_count) {
				// ToDo. substr required
				// field.value = field.value.replace(/.$/, '')
				// _event.break_event();
				//
				// ToDo. if overflowed, we do nothing but a coloring...
				var character_counter = document.getElementById(field.id + "_character_count");
				character_counter.style.color = "red";
				character_counter.innerHTML = cur_count;
			} else {
				var character_counter = document.getElementById(field.id + "_character_count");
				character_counter.innerHTML = cur_count;
			}
		}));
	}
};

var RenderAnswersByPagination = function(_e) {
	var eventor = new Eventor(_e);
	ReplaceLayerContent(/[^\/]*$/.exec(eventor._node().href));
};

var detectCurrentContainer = function() {
	var pcw = document.getElementById(CONTAINER_WRAPPER_ID);
	if(pcw) {
		try {
			var clue_left_position = pcw.style.left.replace(/px$/, '');
			var container_index = Math.abs(Math.floor(clue_left_position / STEP_X));
			// if((clue_left_position % STEP_X) > (STEP_X / 2)) container_index = Number(container_index) + 1;
			ACTIVATED_CONTAINER_ID = PRAYER_CONTAINERS[container_index].id;
		} catch(e) {
			ACTIVATED_CONTAINER_ID = PRAYER_CONTAINERS[0].id;
		}
	}
};

var defineMinX = function() {
	MIN_X = (STEP_X * PRAYER_CONTAINERS.length) * -1;
};












//
// zEventControlller
//
var zEventController = function() {
};

zEventController.prototype.hijack = function(_e) {
	this.eventor = new Eventor(_e);

	var action_type = { keyup: "keyup", DOMMouseScroll: "wheel", mousewheel: "wheel" };
	this["case_" + action_type[this.eventor._type()]]();
}

//
// case methods
zEventController.prototype.case_wheel = function() {
	window["zScroll" + this.eventor._wheel()._direction.caps()](ACTIVATED_CONTAINER_ID, STEP_Y);
};

zEventController.prototype.case_keyup = function() {
	if(["textarea", "input"].include(this.eventor._node().tagName.toLowerCase())) {
	} else {
		if((this.eventor._keycode() == 73 && this.eventor._event().altKey) || (this.eventor._keycode() == 13)) {
			this.activateNewPrayerTopic();
			this.eventor.break_event();
		}
	}
};

zEventController.prototype.activateNewPrayerTopic = function() {
	document.getElementById("prayer_topic").focus();
}
